package org.csu.backofficemanagersys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.backofficemanagersys.domain.Category;
import org.csu.backofficemanagersys.domain.Inventory;
import org.csu.backofficemanagersys.domain.Item;
import org.csu.backofficemanagersys.domain.Product;
import org.csu.backofficemanagersys.mapper.CategoryMapper;
import org.csu.backofficemanagersys.mapper.InventoryMapper;
import org.csu.backofficemanagersys.mapper.ItemMapper;
import org.csu.backofficemanagersys.mapper.ProductMapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemService {
    final ItemMapper itemMapper;
    final ProductMapper productMapper;
    final CategoryMapper categoryMapper;
    final InventoryMapper inventoryMapper;

    @Autowired
    public ItemService(ItemMapper itemMapper, ProductMapper productMapper, CategoryMapper categoryMapper,InventoryMapper inventoryMapper) {
        this.itemMapper = itemMapper;
        this.productMapper = productMapper;
        this.categoryMapper = categoryMapper;
        this.inventoryMapper = inventoryMapper;
    }
    //获取相应的对象
    public Category getCategoryByName(String catid){
        return categoryMapper.selectOne(new QueryWrapper<Category>().eq("catid",catid));
    }
    public List<Category> getAllCategory(){
        return categoryMapper.selectList(null);
    }
    public Product getProductById(String productid){
        return productMapper.selectOne(new QueryWrapper<Product>().eq("productid",productid));
    }
    public List<Product> getAllProduct(){
        return productMapper.selectList(null);
    }
    public Item getItemById(String itemid){
        Item item = itemMapper.selectOne(new QueryWrapper<Item>().eq("itemid",itemid));
        item.setProduct(getProductById(item.getProductid()));
        return item;
    }
    public List<Item> getAllItem(){
        List<Item> itemList = itemMapper.selectList(null);
        for(Item item : itemList){
            item.setProduct(getProductById(item.getProductid()));
            Inventory inventory = inventoryMapper.selectOne(new QueryWrapper<Inventory>().eq("itemid",item.getItemid()));
            item.setQty(inventory.getQty());
        }
        return itemList;
    }
    //对相应的对象修改
    public Boolean updateCategory(@NotNull Category category){
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("catid",category.getCatid());
        return categoryMapper.update(category, updateWrapper) != 0;
    }
    public Boolean updateProduct(@NotNull Product product){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",product.getProductid());
        return productMapper.update(product, queryWrapper) != 0;
    }
    public Boolean updateItem(@NotNull Item item){
        UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemid",item.getItemid());
        return itemMapper.update(item,updateWrapper)!=0;
    }
    //添加相应对象
    public Boolean insertCategory(@NotNull Category category){
        try {
            categoryMapper.insert(category);
        }catch (Exception e){
            return false;
        }
        return true;
   }
    public Boolean insertProduct(@NotNull Product product){
        try {
            productMapper.insert(product);
        }catch (Exception e){
            return false;
        }
        return true;
   }
    public Boolean insertItem(@NotNull Item item){
        try {
            itemMapper.insert(item);
        }catch (Exception e){
            return false;
        }
        return true;
   }
    //删除相应对象
    public Boolean deleteCategory(@NotNull String catid){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catid",catid);
        deleteProductByCat(catid);
        return  categoryMapper.delete(queryWrapper) != 0;
    }
    public Boolean deleteProduct(@NotNull String productid){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productid);
        List<Product> products = productMapper.selectList(queryWrapper);
        for(Product product : products){
            deleteItemByProduct(product.getProductid());
        }
        return productMapper.delete(queryWrapper)!=0;

    }
    public Boolean deleteItem(@NotNull String itemid){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",itemid);
        return itemMapper.delete(queryWrapper)!=0;
    }
    public void deleteProductByCat(String category){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        List<Product> productList = productMapper.selectList(queryWrapper);
        for(Product product : productList){
            deleteItemByProduct(product.getProductid());
        }
        productMapper.delete(queryWrapper);
    }
    public void deleteItemByProduct(String productid){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productid);
        itemMapper.delete(queryWrapper);
    }
    public List<Category> findCategoryLike(String key){
        return categoryMapper.selectList(new QueryWrapper<Category>().like("catid",key));
    }
    public List<Product> findProductLike(String key){
        return productMapper.selectList(new QueryWrapper<Product>().like("productid",key));
    }
    public List<Item> findItemLike(String key){
        List<Item> items = itemMapper.selectList(new QueryWrapper<Item>().like("itemid",key));
        for(Item item :items){
            item.setProduct(getProductById(item.getProductid()));
            Inventory inventory = inventoryMapper.selectOne(new QueryWrapper<Inventory>().eq("itemid",item.getItemid()));
            item.setQty(inventory.getQty());
        }
        return items;
    }

}
